package thelist;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import database.Database;

public class thelistChannel {
	
	public static String listChannelbyName(int id) {	//get channel tag from database by id
		try {
			Connection conn = Database.getConnection();
			Statement stmt;
			stmt = conn.createStatement();
			
			ResultSet rs = stmt.executeQuery(
					"select tag " +
					"from channel " +
					"where id = " + id);
			
			rs.next();
			String re = rs.getString("tag");
			
			stmt.close();
			conn.close();
			
			return re;
		} 
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static void listThisChannels(PageContext p) {	//show THIS channel list from DB
		try {
			JspWriter out = p.getOut();
			
			Connection conn = Database.getConnection();
			Statement stmt = conn.createStatement();
			
			ResultSet rs = stmt.executeQuery(
					"select id, tag, pic_url " +
					"from channel " +
					"where allow_this = 1 and enable = 1");
			
			while(rs.next())
			{
				out.println("<a href=/IFTTT/createtaskThis?thisid=" + Integer.parseInt(rs.getString("id")) + ">");
				out.println("<div class='individual_channel'>");
				out.println("<div class='channel'>");
				out.print("<img src='" + rs.getString("pic_url") + "' ");
				out.println("title='" + rs.getString("tag") + "'/>");
				out.println("<div class='name'>");
				out.println(rs.getString("tag"));
				out.println("</div></div></div></a>");
			}
			
			stmt.close();
			conn.close();
			
		} catch (SQLException | NumberFormatException | IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void listThatChannels(PageContext p) {	//show THAT channel list from DB
		try {
			JspWriter out = p.getOut();
			
			Connection conn = Database.getConnection();
			Statement stmt = conn.createStatement();
			
			ResultSet rs = stmt.executeQuery(
					"select id, tag, pic_url " +
					"from channel " +
					"where allow_that = 1 and enable = 1");
			
			while(rs.next())
			{
				out.println("<a href=/IFTTT/createtaskThat?thatid=" + Integer.parseInt(rs.getString("id")) + ">");
				out.println("<div class='individual_channel'>");
				out.println("<div class='channel'>");
				out.print("<img src = '" + rs.getString("pic_url") + "' ");
				out.println("title = '" + rs.getString("tag") + "'/>");
				out.println("<div class='name'>");
				out.println(rs.getString("tag"));
				out.println("</div></div></div></a>");
			}
			
			stmt.close();
			conn.close();
			
		} catch (SQLException | NumberFormatException | IOException e) {
			e.printStackTrace();
		}		
	}
	
	public static void listChannelsForUsr(PageContext p) {	//show visible channel
		JspWriter out = p.getOut();
		
		try {

			Connection conn = Database.getConnection();
			Statement stmt = conn.createStatement();
			
			ResultSet rs = stmt.executeQuery(
					"select tag, pic_url " +
					"from channel " +
					"where enable = 1"
			);
			
			while ( rs.next() ) {
				out.println(
					"<div class='individual_channel'>" + "\n" +
					"	<div class='channel'>" + "\n" +
					"		<img src='" + rs.getString("pic_url") + "' title='" + rs.getString("tag") + "' />" + "\n" +
					"		<div class='name'>" + "\n" +
					"			" + rs.getString("tag") + "\n" +
					"		</div>" + "\n" +
					"	</div>" + "\n" +
					"</div>"
				);
			}
			
			stmt.close();
			conn.close();
			
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void listChannelsForAdmin(PageContext p) {	//show all channel

		JspWriter out = p.getOut();
		
		try {

			Connection conn = Database.getConnection();
			Statement stmt = conn.createStatement();
			
			ResultSet rs = stmt.executeQuery(
					"select * " +
					"from channel"
			);
			
			while ( rs.next() ) {
				boolean enable = rs.getBoolean("enable");
				double opacity = 0.2;
				if ( enable ) {
					opacity = 1;
				}
				
				out.println(
					"<a href='ChangeChannel?id=" + rs.getInt("id") + "&enable=" + !rs.getBoolean("enable") + "'>" + "\n" +
					"<div class='individual_channel' style='opacity:" + opacity + "'>" + "\n" +
					"	<div class='channel'>" + "\n" +
					"		<img src='" + rs.getString("pic_url") + "' title='" + rs.getString("tag") + "' />" + "\n" +
					"		<div class='name'>" + "\n" +
					"			" + rs.getString("tag") + "\n" +
					"		</div>" + "\n" +
					"	</div>" + "\n" +
					"</div>" + "\n" +
					"</a>"
				);
			}
			
			stmt.close();
			conn.close();
			
			
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
		
	}
}
